import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
// 导入全局样式表
import '../assets/css/global.css'
// 导入home组件
import Home from '../components/home.vue'
// 导入Welcome组件 设置为home组件的子组件
import Welcome from '../components/Welcome.vue'
// 导入用户管理组件
import Users from '../components/user/users.vue'
// 导入权限列表组件
import Rights from '../components/power/Rights.vue'
// 导入角色列表组件
import Roles from '../components/power/Roles.vue'
// 导入商品列表组件
import Cate from '../components/goods/Cate.vue'
// 导入分类参数组件
import Params from '../components/goods/Params.vue'
// 导入商品列表组件
import List from '../components/goods/List.vue'
// 导入添加商品路由
import Add from '../components/goods/Add.vue'
// 导入订单列表组件
import Order from '../components/Order/Order.vue'
// 导入报表组件
import Report from '../components/report/Report.vue'
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: List },
        { path: '/goods/add', component: Add },
        { path: '/orders', component: Order },
        { path: '/reports', component: Report }
      ]
    }
  ]
})
// 挂载路由导航守卫
// to表示要访问的路径
// form表示从那个路径跳转而来
// next方法表示放行 next('/**)强制跳转
router.beforeEach((to, from, next) => {
  // 如果访问的是登录页面直接放行  这里要写全等
  if (to.path === '/login') return next()
  // 如果访问的home页面通过token判断是否是登录状态
  const tokenStr = window.sessionStorage.getItem('token')
  // token如果不存在强制到登录页面
  if (!tokenStr) return next('/login')
  // token存在放行
  next()
})
export default router
